﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Design;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using GISCore.Geometry.IO;
using GISCore.Geometry;
using System.Threading;



namespace VisualTools
{
    public partial class Form1 : Form
    {
        private Layer layer_base;
        private Layer layer_over;
        //private StreamWriter sw; 
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            IFileHandler handler_base = new GMLFileHandler(1);
            string filename = "../../../GISUnitTests/Test Data/layerA_parallel.gml";
            layer_base = handler_base.Read(filename);
            string filename1 = "../../../GISUnitTests/Test Data/layerB_parallel.gml";
            layer_over = handler_base.Read(filename1);
            //retrieveSmallSubset("polygon1subset", 15, 15, "layerA_parallel",0,0);
            //retrieveSmallSubset("polygon1subset", 17, 17, "layerB_parallel",0.002,-0.001);
            //sw = new StreamWriter("../../../GISUnitTests/Test Data/test.txt", false, System.Text.Encoding.GetEncoding("utf-8"));
            //retrieveSmallSubset();
        }

        private void Form1_paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics ; 
            Pen MyPen1 = new Pen(Color.Red);
            Pen MyPen2 = new Pen(Color.Black);
            Pen MyPen3 = new Pen(Color.Blue);
            Pen MyPen4 = new Pen(Color.Chocolate);

            //showLayer(g, MyPen1, MyPen1, MyPen1, MyPen2, layer_base,0,1,186);
            showLayer(g, MyPen3, MyPen3, MyPen3, MyPen4, layer_base,0,1,100);
            showLayer(g, MyPen1, MyPen1, MyPen1, MyPen2, layer_over, 0, 1, 1);            
            //sw.Close();
        }

        private double transferX(double x,int flag)
        {
            double tempX=0.0;
            if(flag==0){
            return x;
            }
            else if(flag==1){
                tempX=((x - 35.9890083925622) * 2000);
            }
            else if (flag == 2)
            {
                tempX = x * 10;
            }
            else if (flag == 3)
            {
                tempX = 3*((x - 35.9890083925622) * 2000);
            }
            return tempX;
       }

        private double transferY(double y, int flag,int height)
        {
            double tempY = 0.0;
            if (flag == 0)
            {
                return y;
            }
            else if (flag == 1)
            {
                tempY = height-((y + 79.9986683728864) * 2000);
            }
            else if (flag == 2)
            {
                tempY = y * 10;
            }
            else if (flag == 3)
            {
                tempY = 3*(height-((y + 79.9986683728864) * 2000));
            }
            return tempY;
        }
        private void retrieveSmallSubset(string source,int start,int end,string target,double x,double y)
        {
            int i = 0;
            IFileHandler handler_source = new GMLFileHandler(1);
            IFileHandler handler_target = new GMLFileHandler(2);
            string filename1 = "../../../GISUnitTests/Test Data/"+source+".gml";
            string filename2 = "../../../GISUnitTests/Test Data/" + target + ".gml";
            layer_base = handler_source.Read(filename1);
            Layer newLayer = new Layer(1, new List<Polygon>(), new GISCore.Geometry.Point(0, 0), new GISCore.Geometry.Point(0, 0), 0.0);
            foreach (var p in layer_base.GetPolygons())
            {
                i++;
                if (i >= start)
                {
                    foreach(var pt in p.getPoints(true))
                    {
                        pt.setX(pt.X + x);
                        pt.setY(pt.Y + y);
                    }
                    newLayer.AddPolygon(p);
                }
                if (i == end)
                {
                    break;
                }
            }
            
            //string filename = "../../../GISUnitTests/Test Data/layerA_parallel.gml";
            handler_target.Write(newLayer, filename2);
            
        }
        private void showLayer(Graphics g, Pen MyPen1, Pen MyPen2, Pen MyPen3,Pen MyPen4,Layer layer1,int sleepTime,int startPoly,int endPoly)
        {
            int i = 0;
            int numOfHoles = 0;
            int height = 780;
            int flag=3;
            //g.DrawLine(MyPen, (float)236.0788, (float)120.1885, (float)236.0788, (float)120.1885);
            foreach (var p in layer1.GetPolygons())
            {
                i++;
                if (p.holes.Count != 0)
                {
                    numOfHoles++;
                }
                if (i < startPoly)
                {
                    continue;
                }
                foreach (var l in p.getLines())
                {
                    //886.01,1846.17,883.71,1817.627
                    //Console.WriteLine(((float)((l.Start.getX() - 35.9890083925622) * 2000)) + "," + ((float)((l.Start.getY() + 79.9986683728864) * 2000)) + "," + ((float)((l.End.getX() - 35.9890083925622) * 2000))+ "," + ((float)((l.End.getY() + 79.9986683728864) * 2000)));
                    Console.WriteLine(((float)transferX(l.Start.getX(), flag)) + "," + ((float)transferY(l.Start.getY(), flag,height)) + "," + ((float)transferX(l.End.getX(), flag)) + "," + ((float)transferY(l.End.getY(), flag,height)));

                    if (i % 3 == 0)
                    {
                        g.DrawLine(MyPen1, ((float)transferX(l.Start.getX(), flag)), ((float)transferY(l.Start.getY(), flag, height)), ((float)transferX(l.End.getX(), flag)), ((float)transferY(l.End.getY(), flag, height)));
                    }
                    else if (i % 3 == 1)
                    {
                        g.DrawLine(MyPen2, ((float)transferX(l.Start.getX(), flag)), ((float)transferY(l.Start.getY(), flag, height)), ((float)transferX(l.End.getX(), flag)), ((float)transferY(l.End.getY(), flag, height)));

                    }
                    else
                    {
                        g.DrawLine(MyPen3, ((float)transferX(l.Start.getX(), flag)), ((float)transferY(l.Start.getY(), flag, height)), ((float)transferX(l.End.getX(), flag)), ((float)transferY(l.End.getY(), flag, height)));

                    }
                    Thread.Sleep(sleepTime);
                    //sw.WriteLine(l.Start.getX()+"\t"+l.Start.getY());
                    //sw.WriteLine(l.End.getX() + "\t" + l.End.getY());
                }
                foreach (var h in p.holes)
                {
                    foreach (var l in h.getLines())
                    {
                        g.DrawLine(MyPen4, ((float)transferX(l.Start.getX(), flag)), ((float)transferY(l.Start.getY(), flag, height)), ((float)transferX(l.End.getX(), flag)), ((float)transferY(l.End.getY(), flag, height)));
                    }
                }
                if (i == endPoly)
                {
                    break;
                }
            }
            Console.WriteLine("numOfHoles= " + numOfHoles);
        }
    }
}
